package io.renren.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.dao.CategoryDao;
import io.renren.common.dao.DishDao;
import io.renren.common.dao.DishFlavorDao;
import io.renren.common.dao.SetmealDishDao;
import io.renren.common.dto.CategoryDTO;
import io.renren.common.dto.DishDTO;
import io.renren.common.entity.CategoryEntity;
import io.renren.common.entity.DishEntity;
import io.renren.common.entity.DishFlavorEntity;
import io.renren.common.redis.RedisUtils;
import io.renren.common.service.DishService;
import io.renren.common.service.impl.CrudServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜品管理
 *
 * @author yidian ${email}
 * @since 1.0.0 2022-11-06
 */
@Service
public class DishServiceImpl extends CrudServiceImpl<DishDao, DishEntity, DishDTO> implements DishService {

    @Autowired
    private DishDao dishDao;
    @Autowired
    private DishFlavorDao dishFlavorDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private SetmealDishDao setmealDishDao;



    @Override
    public QueryWrapper<DishEntity> getWrapper(Map<String, Object> params)  {
        String name = (String)params.get("name");
        String code = (String)params.get("code");

        QueryWrapper<DishEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        wrapper.eq(StringUtils.isNotBlank(code), "code", code);

        String sPrice =(String)params.get("minPrice");
        String bPrice =(String)params.get("maxPrice");
        wrapper.ge(StringUtils.isNotBlank(sPrice), "price", sPrice);
        wrapper.le(StringUtils.isNotBlank(bPrice), "price", bPrice);

        return wrapper;
    }

    @Override
    public void getByWithFlavor(DishDTO dishDTO,Long id) {
        LambdaQueryWrapper<DishFlavorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavorEntity::getDishId,id);
        List<DishFlavorEntity> dishFlavorEntityList = dishFlavorDao.selectList(queryWrapper);
        dishDTO.setDishFlavors(dishFlavorEntityList);
    }

    /**
     * 根据分类类型查询
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DishDTO> findDishByCategoryId(DishDTO dishDTO) {
        List<DishDTO> dishDtoList =null;

        LambdaQueryWrapper<DishEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dishDTO.getCategoryId() != null,DishEntity::getCategoryId,dishDTO.getCategoryId());
        queryWrapper.orderByDesc(DishEntity::getSort).orderByDesc(DishEntity::getUpdateDate);
        List<DishEntity> dishes = dishDao.selectList(queryWrapper);
        dishDtoList = new ArrayList<>();

        for (DishEntity dish : dishes) {
            DishDTO dto = new DishDTO();
            BeanUtils.copyProperties(dish,dto);
            Long categoryId = dish.getCategoryId();
            CategoryEntity category = categoryDao.selectById(categoryId);
            if (category != null){
                String name = category.getName();
                dto.setCategoryName(name);

            }

            //当前菜品ID
            getByWithFlavor(dto,dto.getId());
            dishDtoList.add(dto);

        }


        return dishDtoList;
    }


}